package yxy.game.pm2.chat.module;

import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.area.AreaGroupBean;
import yxy.game.pm2.bean.area.ChatChannelType;
import yxy.game.pm2.bean.cluster.ClusterBean;

public class ChatHandle {
	static public void register(String playerCode) {
		PlayerBean playerBean = PlayerBean.get(playerCode);
		ServerBean serverBean = ServerBean.get(playerBean.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
		ClusterBean clusterBean = ClusterBean.get(areaBean.getClusterId()); //专服

		// 注册世界频道
		ChatChannel_World.get(areaBean.getGroupId()).register(playerBean);
		// 注册系统频道
		ChatChannel_System.get(clusterBean.getClusterId()).register(playerBean);
		// 注册滚动频道
		ChatChannel_Scroll.get(clusterBean.getClusterId()).register(playerBean);
		// 注册区频道
		ChatChannel_Area ccarea = ChatChannel_Area.get(playerBean);
		ccarea.register(playerBean);
		// 注册联盟频道
		ChatChannel_Union ccunion = ChatChannel_Union.get(playerBean.getCode());
		if (ccunion != null) {
			ccunion.register(playerBean);
		}
		// 注册同省频道
		ChatChannel_HomeTown cchome = ChatChannel_HomeTown.get(clusterBean.getClusterId(), playerBean.getTown());
		if (cchome != null) {
			cchome.register(playerBean);
		}
	}

	static public void send(String playerCode, ChatChannelType channelType, String text) {
		PlayerBean playerBean = PlayerBean.get(playerCode);
		ServerBean serverBean = ServerBean.get(playerBean.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
		ClusterBean clusterBean = ClusterBean.get(areaBean.getClusterId()); //专服

		if ( !ChatChannelType.checkPlayerChannelType(channelType) ) return;

		BaseChatChannelModule chatChannelModule = getChannel(
				clusterBean.getClusterId(),
				areaBean.getGroupId(),
				areaBean.getAreaId(),
				playerBean.getCode(),
				channelType
		);
		if (chatChannelModule == null) {
			return;
		}
		ChatMsgPlayer msg = ChatMsg.create(playerCode, channelType, text);
		chatChannelModule.send(msg);
	}

	static public void sendInner(String playerCode, int areaId, ChatChannelType channelType, String text) {
		PlayerBean playerBean = PlayerBean.get(playerCode);
		ServerBean serverBean = ServerBean.get(playerBean.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
		ClusterBean clusterBean = ClusterBean.get(areaBean.getClusterId()); //专服

		BaseChatChannelModule chatChannelModule = getChannel(
				clusterBean.getClusterId(),
				areaBean.getGroupId(),
				areaBean.getAreaId(),
				playerBean.getCode(),
				channelType
		);
		if (chatChannelModule == null) {
			return;
		}
		switch (channelType) {
		case CHAT_AREA:
		case CHAT_UNION:
		case CHAT_REGION:
		case CHAT_WORLD: {
			ChatMsgPlayer msg = ChatMsg.create(playerCode, channelType, text);
			chatChannelModule.send(msg);
		}
			break;
		case CHAT_SYS:
		case CHAT_SCROLL: {
			ChatMsg msg = ChatMsg.create(channelType, text);
			chatChannelModule.send(msg);
		}
			break;
		default:
			return;
		}
	}

	static public BaseChatChannelModule getChannel(int clusterId, int areaGroupId, int areaId, String playerCode, ChatChannelType channelType) {
		switch (channelType) {
		case CHAT_WORLD:
			return ChatChannel_World.get(areaGroupId);
		case CHAT_SYS:
			return ChatChannel_System.get(clusterId);
		case CHAT_SCROLL:
			return ChatChannel_Scroll.get(clusterId);
		case CHAT_AREA: {
			PlayerBean playerBean = PlayerBean.get(playerCode);
			if (playerBean == null) {
				return ChatChannel_Area.get(areaId);
			}
			return ChatChannel_Area.get(playerBean);
		}
		case CHAT_UNION: {
			return ChatChannel_Union.get(playerCode);
		}
		case CHAT_REGION: {
			PlayerBean playerBean = PlayerBean.get(playerCode);
			return ChatChannel_HomeTown.get(clusterId, playerBean);
		}
		default:
			return null;
		}
	}
}
